//
//  UITextField+Extension.m
//  CustomFramework
//
//  Created by 侯歌 on 2023/1/30.
//

#import "UITextField+Extension.h"
#import <objc/runtime.h>

@implementation UITextField (Extension)

- (NSString *)placeholderText {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPlaceholderText:(NSString *)placeholderText {
    objc_setAssociatedObject(self, @selector(placeholderText), placeholderText, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self bwSetPlaceholder];
}

- (CGFloat)placeholderFontSize {
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

- (void)setPlaceholderFontSize:(CGFloat)placeholderFontSize {
    objc_setAssociatedObject(self, @selector(placeholderFontSize), @(placeholderFontSize), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self bwSetPlaceholder];
}

- (UIColor *)placeholderColor {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setPlaceholderColor:(UIColor *)placeholderColor {
    objc_setAssociatedObject(self, @selector(placeholderColor), placeholderColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self bwSetPlaceholder];
}

- (void)bwSetPlaceholder {
    if ([self placeholderText] == nil) {
        return;
    }
    UIFont *font = [UIFont systemFontOfSize:[self placeholderFontSize] ? [self placeholderFontSize] : 14];
    UIColor *color = [self placeholderColor] ? [self placeholderColor] : [UIColor lightGrayColor];
    NSDictionary *dic = @{
        NSFontAttributeName: font,
        NSForegroundColorAttributeName: color
    };
    NSAttributedString *placeHolderString = [[NSAttributedString alloc] initWithString:[self placeholderText] attributes:dic];
    self.attributedPlaceholder = placeHolderString;
}

@end
